package com.shundr.shipper.common;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shundr.shipper.R;
import com.shundr.shipper.base.BaseActivity;
import com.shundr.shipper.common.adapter.AreaAdapter;
import com.shundr.shipper.common.dao.AreaDao;
import com.shundr.shipper.common.model.AreaInfo;
import com.shundr.shipper.common.util.ToastHelper;
import com.shundr.shipper.frame.util.StringUtil;

/**
 * 单选城市加历史
 * AreaGridActivity
 * 
 * @author larry 2015-7-21 上午10:04:46 
 * @version 1.0.0
 *
 */
public class DetailAreaGridActivity extends BaseActivity {
	
	private LinearLayout mLayoutHistory;
	
	private Button btnBack;
	private TextView tvName;
	private Button btnConfirm;
	
	private GridView mGridView;
	
	private List<AreaInfo> mAreaList = new ArrayList<AreaInfo>();
	private AreaDao mDao;
	private AreaAdapter mAdapter;
	
	private AreaInfo mInfo = null;
	
	private String mProvince = "";
	private String mCity = "";
	private String mArea = "";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_area_grid);
		mDao = new AreaDao();
		String title = getIntent().getStringExtra("title");
		if(!StringUtil.isNull(title)){
			setTitle(title);
		}
		initView();
		initData();
	}
	
	private void initData(){
		tvName.setText("请选择");
		mLayoutHistory.setVisibility(View.GONE);
		
		mAreaList.addAll(mDao.getAreasByParent("00"));
		if(mAreaList.size() > 0){
			mAreaList.remove(0);
			mAdapter.notifyDataSetChanged();
		}
	}
	
	private void initView(){
		mLayoutHistory = (LinearLayout)findViewById(R.id.layout_1);
		
		btnBack = (Button)findViewById(R.id.btn_back);
		tvName = (TextView)findViewById(R.id.tv_name);
		btnConfirm = (Button)findViewById(R.id.btn_confim);
		btnBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				mInfo = mDao.getAreaByParentCode(mInfo.getParentCode());
				if(StringUtil.isNull(mInfo.getCode())){
					btnBack.setVisibility(View.GONE);
				}else{
					btnBack.setVisibility(View.VISIBLE);
				}
				tvName.setText(mInfo.getName());
				if(StringUtil.isNull(mInfo.getCode())){
					mAreaList.clear();
					mAreaList.addAll(mDao.getAreasByParent("00"));
					mAreaList.remove(0);
					mAdapter.notifyDataSetChanged();
				}else{
					initGridView();
				}
			}
		});
		
		btnConfirm.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (mInfo == null) {
					ToastHelper.toastShort(mContext, "请先选择一个地址");
				} else {
					if(mInfo.getCode().length() < 6){
						ToastHelper.toastShort(mContext, "请选择到区");
						return;
					}
					Intent intent = new Intent();
					intent.putExtra("addr", mProvince + mCity + mArea);
					intent.putExtra("areaCode", mInfo.getCode());
					setResult(502,intent);
					finish();
				}
			}
		});
		
		mAdapter = new AreaAdapter(mContext, mAreaList);
		mGridView = (GridView) findViewById(R.id.gridview);
		mGridView.setAdapter(mAdapter);
		
		mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				mInfo = mAreaList.get(position);
				if(StringUtil.isNull(mInfo.getCode())){
					btnBack.setVisibility(View.GONE);
				}else{
					btnBack.setVisibility(View.VISIBLE);
				}
				if(mInfo.getCode().length() == 2){
					mProvince = mInfo.getName();
				}else if(mInfo.getCode().length() == 4){
					mCity = mInfo.getName();
				}else{
					mArea = mInfo.getName();
				}
				tvName.setText(mInfo.getName());
				if(!StringUtil.isNull(mInfo.getCode())){
					initGridView();
				}
			}
		});
	}
	
	private void initGridView(){
		List<AreaInfo> list = new ArrayList<AreaInfo>();
		if(mInfo == null ||  StringUtil.isNull(mInfo.getCode())){
			list.addAll(mDao.getAreasByParent("00"));
			list.remove(0);
		}else{
			list.addAll(mDao.getAreasByParent(mInfo.getCode()));
		}
		if(list.size() > 0){
			mAreaList.clear();
			mAreaList.addAll(list);
			mAdapter.notifyDataSetChanged();
		}
	}

}
